package com.xindacloud.sms.adapter.component;

import com.nexmo.client.NexmoClientException;
import com.nexmo.client.insight.BasicInsightResponse;
import com.xindacloud.sms.adapter.constant.CommonConstant;
import com.xindacloud.sms.adapter.exception.ServiceException;
import com.xindacloud.sms.adapter.service.factory.NexmoClientFactory;
import com.xindacloud.sms.adapter.utils.NexmoStatusHelp;
import com.xindacloud.sms.adapter.utils.UnionResultHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Component
@Slf4j
public class BasicInsight {

    private final NexmoClientFactory nexmoClientFactory;

    @Autowired
    public BasicInsight(NexmoClientFactory nexmoClientFactory) {
        this.nexmoClientFactory = nexmoClientFactory;
    }

    public BasicInsightResponse basicInsightByNumberAndCountry(String number, String country) {

        try {
            final BasicInsightResponse basicNumberInsight = nexmoClientFactory.getRandomNexmoClient()
                    .getInsightClient().getBasicNumberInsight(number, country);
            return Optional.of(basicNumberInsight)
                    .filter(rs -> rs.getStatus().getInsightStatus() == 0)
                    .orElseThrow(() -> new ServiceException(NexmoStatusHelp.NumberInsightStatus
                            .fromInt(basicNumberInsight.getStatus().getInsightStatus())
                            .getMessage(), UnionResultHelp.StatusEnum.ParamsFail.code()));
        } catch (NexmoClientException e) {
            log.error("号码验证异常：{}", e.getMessage(), e);
            throw new ServiceException("远程服务验证号码异常", UnionResultHelp.StatusEnum.BadGateway.code());
        }

    }

    public BasicInsightResponse basicInsightByNumber(String number) {

        try {

            final BasicInsightResponse basicNumberInsight = nexmoClientFactory.getRandomNexmoClient()
                    .getInsightClient().getBasicNumberInsight(number);

            return Optional.of(basicNumberInsight)
                    .filter(rs -> rs.getStatus().getInsightStatus() == 0)
                    .orElseThrow(() -> new ServiceException(NexmoStatusHelp.NumberInsightStatus
                            .fromInt(basicNumberInsight.getStatus().getInsightStatus())
                            .getMessage(), UnionResultHelp.StatusEnum.ParamsFail.code()));

        } catch (NexmoClientException e) {
            log.error("号码验证异常：{}", e.getMessage(), e);
            throw new ServiceException("远程服务验证号码异常", UnionResultHelp.StatusEnum.BadGateway.code());
        }
    }


    public List<String> validNumber(List<String> recipientList) {
        List<String> invalidNumbers = new ArrayList<>();
        try {
            for (String recipient : recipientList) {

                final BasicInsightResponse basicNumberInsight = nexmoClientFactory.getRandomNexmoClient()
                        .getInsightClient().getBasicNumberInsight(recipient);
                if (!Objects.equals(0, basicNumberInsight.getStatus().getInsightStatus())) {
                    invalidNumbers.add(recipient);
                }
            }
        } catch (NexmoClientException e) {
            log.error("号码验证异常：{}", e.getMessage(), e);
            throw new ServiceException("远程服务验证号码异常", UnionResultHelp.StatusEnum.BadGateway.code());
        }
        return invalidNumbers;
    }

    public boolean checkNumberValid(String toNumber) {

        try {
            String[] numbers = toNumber.split(CommonConstant.SEPARATOR_STR);
            int i = 0;
            while (i < numbers.length) {
                BasicInsightResponse basicNumberInsight = nexmoClientFactory.getRandomNexmoClient().getInsightClient().getBasicNumberInsight(numbers[i]);
                Optional.of(basicNumberInsight)
                        .filter(rs -> rs.getStatus().getInsightStatus() == 0)
                        .orElseThrow(() -> new ServiceException(NexmoStatusHelp.NumberInsightStatus
                                .fromInt(basicNumberInsight.getStatus().getInsightStatus())
                                .getMessage(), UnionResultHelp.StatusEnum.ParamsFail.code()));

                i++;
            }
        } catch (NexmoClientException e) {
            log.error("号码验证异常：{}", e.getMessage(), e);
            throw new ServiceException("远程服务验证号码异常", UnionResultHelp.StatusEnum.BadGateway.code());
        }
        return true;
    }
}
